zhuaguiMgr = {}
QMPlugin = zhuaguiMgr
function zhuaguiMgr.init(maxCount)
    zhuaguiMgr.maxCount = maxCount or 999999
    zhuaguiMgr.curCount = 0
    if boot.platform == 1 then
        zhuaguiMgr.paraConfig = {
            ["钟馗抓鬼领取任务按钮"] = {35,910,751,1849,"F4FCFE","9|-44|F4FCFE,13|52|F4FCFE,-8|69|F4FCFE,-331|55|70CF7B,-338|-129|70CF7B,-337|-169|70CF7B,-347|-258|F4FCFE",4,0.9},
            ["经验极低确认按钮"] = { 103,346,993,1597,"4F58D9","-22|84|454EDC,8|-62|545CDE,164|93|C5E9FF,170|392|192A72,162|423|192A72,173|450|192A72,153|466|26397D,366|589|3F4983,328|600|91D3ED",0,0.9},
            ["一轮结束是否继续"] = { 283,1066,826,1442,"4D56D8","3|-68|4F58D9,5|151|4F59DA,-64|28|C5E9FF,276|16|C5E9FF,165|-15|192A72",0,0.9},
            ["申请或者特殊任务按钮"] = { 122,439,593,1362,"48BD55","8|-19|54CA5B,22|19|64D960,13|-96|637BF4,22|-112|687BF2,0|-87|5A6DDA",4,0.9},
            ["队伍缺人"] = {326,1549,805,1875,"475CB7","4|-31|4A5FBA,2|28|485EB9,-23|2|3C51AA,31|-4|566CCA,29|-32|B0DBF7,-24|24|AEDBF7,-250|-14|C0EBFF,215|-2|CAECFF,-62|120|B6D9F4",3,0.9},
            ["抓鬼任务确定按钮"] = {52,1353,258,1670,"4D56D8","5|-106|505ADB,2|102|4F58D9,22|-5|5D64E7,-20|-4|444EDC,3|-42|D1F0FF,-2|-26|D1F0FF,-1|18|D1F0FF,-11|39|D1F0FF,16|29|D1F0FF",0,0.9},
            ["抓鬼任务按钮"] = {35,229,916,741,"1A2C73","6|-28|1B2D75,-8|-41|182A72,-12|-113|BCE0FD,-3|63|182A72,14|72|192A72,-2|125|5176B6,-12|136|5075B6,-3|145|5176B7",0,0.9},
            ["自动匹配按钮"] = {38,1309,179,1904,"D1F0FF","-10|-114|4A53D4,22|-20|6067E9,-24|-17|444EDC,3|97|515ADC,3|-62|D1F0FF,-16|-68|D1F0FF,13|14|D1F0FF",0,0.9},
            ["组队抓鬼任务"] = { 921,285,1062,842,"ACDEFE","18|-36|B0E0FE,9|73|F4FCFE,-8|74|F4FCFE,3|109|F4FCFE,16|113|F4FCFE,5|141|F4FCFE,-8|149|F4FCFE,1|168|F4FCFE,16|180|F4FCFE",0,0.9},
            ["抓鬼前往任务按钮"] = { 25,1052,1073,1910,"182971","6|-27|182971,2|-57|A2D7F7,39|-415|192A72,31|-386|192A72,36|-348|192B72,39|-320|192A72,9|-497|CF4C6A",0,0.9},
            ["便捷组队抓鬼任务按钮"] = { 32,210,1003,691,"8ECCFB","8|90|5075B6,-3|101|5277B8,0|-10|182A72,16|-64|182A72,-13|35|182A72,12|58|8ECCFB,-4|71|243A7F,-14|43|182A72,15|-64|182A72",0,0.9},
            ["便捷组队开始匹配"] = { 66,1376,206,1653,"4D56D7","6|-71|D1F0FF,19|-68|D1F0FF,-10|-68|D1F0FF,-8|30|D1F0FF,4|32|D1F0FF,19|32|D1F0FF,10|109|525ADC,0|-106|4D56D6,20|48|D1EFFF",0,0.9},
            ["便捷组队选中抓鬼"] = { 61,255,883,662,"8AB9FB","-1|-41|182A72,-12|-67|182A72,17|21|182A72,-11|33|182A72,4|47|182A72,16|67|182A72,3|70|182A72,6|112|8AB9FB,1|-182|88B7FB",0,0.9},
        }
    else
        zhuaguiMgr.paraConfig = {
            ["抓鬼任务"] = {485,1071,522,1218,"26B3F9","-5|-3|26A2DD,-10|0|00A5FF,-16|-5|00A5FF",0,0.9},
        }
    end
end


function zhuaguiMgr.setMode(mode)
    zhuaguiMgr.mode = mode
end

-- 判断身上是否有抓鬼任务
local function checkTaskExists()
    local para = zhuaguiMgr.paraConfig["抓鬼任务"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 便捷组队抓鬼任务按钮
local function checkFastZhuaguiBtn()
    local para = zhuaguiMgr.paraConfig["便捷组队抓鬼任务按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 便捷组队开始匹配
local function checkFastPipei()
    local para = zhuaguiMgr.paraConfig["便捷组队开始匹配"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 便捷组队选中抓鬼
local function checkFastZhuaguiSelected()
    local para = zhuaguiMgr.paraConfig["便捷组队选中抓鬼"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 经验极低确认按钮
local function checkExpLimit()
    local para = zhuaguiMgr.paraConfig["经验极低确认按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end
-- 组队抓鬼任务
local function checkTeamZhuagui()
    local para = zhuaguiMgr.paraConfig["组队抓鬼任务"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end
-- 队伍缺人
local function checkTeamNotFull()
    local para = zhuaguiMgr.paraConfig["队伍缺人"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 判断是否继续对话框
local function checkZhuaguijixu()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = zhuaguiMgr.paraConfig["一轮结束是否继续"]
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end,3,500)
    return rtn1,rtn2,rtn3
end

-- 自动匹配按钮
local function checkPipeiBtn()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = zhuaguiMgr.paraConfig["自动匹配按钮"]
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end,3,500)
    return rtn1,rtn2,rtn3
end

-- 判断抓鬼任务按钮
local function checkZhuaguiBtn()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = zhuaguiMgr.paraConfig["钟馗抓鬼领取任务按钮"]
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end,3,500)
    return rtn1,rtn2,rtn3
end

-- 判断是否有入队申请或者特殊按钮
local function checkApplyTeamBtn()
    local para = zhuaguiMgr.paraConfig["申请或者特殊任务按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 抓鬼任务确定按钮
local function checkZhuaguiComfirmBtn()
    local rtn1,rtn2,rtn3 = utils.retry(function()
        local para = zhuaguiMgr.paraConfig["抓鬼任务确定按钮"]
        local itemX,itemY = game.findMultiColor(utils.unpack(para))
        if itemX == -1 or itemY == - 1 then
            return false,itemX,itemY
        else
            return true,itemX,itemY
        end
    end,2,500)
    return rtn1,rtn2,rtn3
end

-- 抓鬼按钮是否可见
local function findZhuaguiTaskBtn()
    local para = zhuaguiMgr.paraConfig["抓鬼任务按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

-- 抓鬼前往任务按钮
local function findZhuaguiGoBtn()
    local para = zhuaguiMgr.paraConfig["抓鬼前往任务按钮"]
    local itemX,itemY = game.findMultiColor(utils.unpack(para))
    if itemX == -1 or itemY == - 1 then
        return false,itemX,itemY
    else
        return true,itemX,itemY
    end
end

local function checkMemTeam()
    if game.IsFuben() then
        mlog.info("正在副本中...")
        game.delay(2000)
        while game.IsFuben() do
            mlog.info("等待副本结束...")
            game.delay(1000)
        end
        mlog.info("退出副本")
        game.delay(1000)
    else
        mlog.info("不在副本中...")
        if not game.IsDisplayDead(2000) then
            mlog.info("正在抓鬼...")
            return true
        end
    end

    mlog.info("检查队伍...")
    local show = game.OpenTeamPanel()
    if show then
        mlog.info("打开队伍面板")
        game.delay(1000)
    end

    local fastTeamBtnShow,x,y = game.fastTeamBtn()
    if fastTeamBtnShow then
        mlog.info("点击便捷组队")
        game.tap(x, y)
        game.delay(1000)
    end

    game.drag(789,469,136,461,500)
    game.delay(2000)
    mlog.info("drag========================")

    local zhuaguiTaskBtnShow,x,y = checkFastZhuaguiBtn()
    if zhuaguiTaskBtnShow then
        mlog.info("点击抓鬼任务按钮")
        game.tap(x, y)
        game.delay(500)
    end

    local fastPipeiBtnShow,x,y = checkFastPipei()
    if fastPipeiBtnShow then
        mlog.info("点击快捷组队匹配按钮")
        game.tap(x, y)
        game.delay(500)
    end

    local closeBtnShow, x, y = game.FindTeamClosePanelBtn()
    if closeBtnShow then
        game.tap(x, y)
        game.delay(800)
    end

    local teamLeaderFlag = game.CheckTeamLeaderFlag()
    local zhuaguiSelectedShow = checkFastZhuaguiSelected()
    while not teamLeaderFlag and zhuaguiSelectedShow do
        mlog.info("等待加入队伍中...")
        local btnShow, x, y = checkPipeiBtn()
        if btnShow then
            game.tap(x, y)
            game.delay(1000)
        end
        game.delay(1000)
        teamLeaderFlag = game.CheckTeamLeaderFlag()
        zhuaguiSelectedShow = checkFastZhuaguiSelected()
    end
    mlog.info("已加入队伍")
    return true
end

local function checkTeam()
    mlog.info("检查队伍...")
    local show = game.OpenTeamPanel()
    if show then
        game.delay(800)
        local createBtn,x,y = game.CheckTeamPanelCreateBtn()
        if createBtn then
            mlog.info("发现队伍创建按钮...")
            game.tap(x, y)
            game.delay(2000)
        end
    end

    local isZhuagui = checkTeamZhuagui()
    if not isZhuagui then
        local showPanel, x, y  = game.CheckTeamPanelOpen()
        if showPanel then
            mlog.info("队伍任务界面开启")
            game.tap(x, y)
            game.delay(2000)
        end
        local btnShow, x, y = findZhuaguiTaskBtn()
        if btnShow then
            mlog.info("队伍任务抓鬼按钮点击")
            game.tap(x, y)
            game.delay(800)
        end

        local btnShow, x, y = checkZhuaguiComfirmBtn()
        if btnShow then
            mlog.info("队伍任务抓鬼按钮确认")
            game.tap(x, y)
            game.delay(800)
        end
    end

    local btnShow, x, y = checkPipeiBtn()
    if btnShow then
        game.tap(x, y)
        game.delay(800)
    end

    local notFull  = checkTeamNotFull()
    local teamLeaderFlag = game.CheckTeamLeaderFlag()

    local waitCount = 0
    while (notFull or not teamLeaderFlag) do
        mlog.info("等待玩家加入队伍中..."..tostring(waitCount))
        local isZhuaguijixuDialogShow,x,y = checkZhuaguijixu()
        if isZhuaguijixuDialogShow then
            mlog.info("等待玩家加入队伍中,抓鬼继续提示出现")
            game.tap(x, y)
            game.delay(500)
        end
        local btnShow, x, y = checkPipeiBtn()
        if btnShow then
            game.tap(x, y)
            game.delay(1000)
        end
        notFull  = checkTeamNotFull()
        mlog.info("notFull="..tostring(notFull))

        -- 超时1分钟重新开始
        if waitCount > 30 then
            game.OtherPanelClose()
            game.delay(500)
            local closeBtnShow, x, y = game.FindTeamClosePanelBtn()
            if closeBtnShow then
                game.tap(x, y)
                game.delay(800)
            end
            mlog.info("等待玩家加入队伍中超时")
            return false
        end
        game.delay(500)
        waitCount = waitCount + 1

        teamLeaderFlag = game.CheckTeamLeaderFlag()
    end
    mlog.info("队伍已满")

    local closeBtnShow, x, y = game.FindTeamClosePanelBtn()
    if closeBtnShow then
        game.tap(x, y)
        game.delay(800)
    end

    return true
end

local function goZhuagui()
    mlog.info("活动抓鬼前往准备")
    local panelShow = game.OpenCampainPanel()
    if panelShow then
        game.delay(2000)
        mlog.info("活动面板打开")
        local btnShow,x,y = game.FindCampainTeamBtn()
        while btnShow do
            mlog.info("活动面板")
            game.tap(x, y)
            game.delay(500)
            btnShow,x,y = game.FindCampainTeamBtn()
        end

        local goBtn,x,y = findZhuaguiGoBtn()
        while goBtn do
            mlog.info("抓鬼前往按钮")
            game.tap(x, y)
            game.delay(500)
            goBtn,x,y = findZhuaguiGoBtn()
        end

        return true
    end

    mlog.info("活动面板没打开")
    return false
end

local function acceptZhuaguiTask()
    local count = 0
    while true do
        local btnShow,x,y = checkZhuaguiBtn()
        if btnShow then
            mlog.info("抓鬼领取按钮出现")
            game.tap(x, y)
            game.delay(800)
            game.tap(x, y)
            game.delay(500)
            if not game.IsDisplayDead(2000) then
                mlog.info("开始抓鬼...当前抓鬼为："..tostring(zhuaguiMgr.curCount))
                return true
            end
        else
            game.delay(1000)
            count = count + 1
            if count > 60 * 5 then
                mlog.info("抓鬼领取等待超时")
                return false
            end
        end
    end

    return true
end

local function zhuaguiStart()
    LuaAuxLib.TracePrint("开始抓鬼 execTask")
    mlog.info("检查关闭其他无用面板")
    game.OtherPanelClose()

    if game.OpenfunPanel() then
        mlog.info("打开功能侧边栏")
        game.delay(1000)
    end

    -- 检查队伍人数
    mlog.info("检查队伍人数")
    local checkTeamFlag = checkTeam()
    if not checkTeamFlag then
        return true
    end
    --有任务直接做任务
    mlog.info("检查任务进度")
    local goFlag = game.ClickTaskPanelZhuaguiBtn()
    if goFlag then
        mlog.info("继续之前未完成进度")
    else
        --没有任务去领任务做
        mlog.info("没有任务去领任务做")
        local goSucc = goZhuagui()
        if goSucc then
            mlog.info("正在前往领取抓鬼任务")
            acceptZhuaguiTask()
        end
    end

    return true
end

local function zhuaguiStartFun()
    local startSucc = zhuaguiStart()
    while not startSucc do
        startSucc = zhuaguiStart()
    end
    return startSucc
end

local function teamLeaderMode()
    zhuaguiMgr.curCount = 1
    zhuaguiStartFun()
    while zhuaguiMgr.curCount < zhuaguiMgr.maxCount do
        mlog.info("检查关闭其他无用面板")
        game.OtherPanelClose()
        if game.OpenfunPanel() then
            mlog.info("打开功能侧边栏")
            game.delay(1000)
        end

        if game.IsFuben() then
            mlog.info("正在副本中...")
            game.delay(2000)
            -- 检查队伍人数
            checkTeam()
            while game.IsFuben() do
                mlog.info("等待副本结束...")
                game.delay(1000)
            end
            mlog.info("退出副本")
            game.delay(1000)
        else
            mlog.info("不在副本中...")
        end

        local isExpLowDialogShow,x,y = checkExpLimit()
        if isExpLowDialogShow then
            mlog.info("经验极低提示出现")
            game.tap(x, y)
            game.delay(500)
            zhuaguiMgr.curCount = zhuaguiMgr.curCount + 1
            mlog.info("开始抓鬼...当前抓鬼为："..tostring(zhuaguiMgr.curCount))
            -- 重新寻路任务
            mlog.info("重新寻路任务")
            goZhuagui()
        end

        local isApplyBtnShow,x,y = checkApplyTeamBtn()
        if isApplyBtnShow then
            mlog.info("申请入队或者特殊事件按钮出现")
            game.tap(x, y)
            game.delay(500)
        end

        local btnShow,x,y = checkZhuaguiBtn()
        if btnShow then
            mlog.info("抓鬼领取按钮出现")
            game.tap(x, y)
            game.delay(500)
        end

        local isZhuaguijixuDialogShow,x,y = checkZhuaguijixu()
        if isZhuaguijixuDialogShow then
            mlog.info("抓鬼继续提示出现")
            game.tap(x, y)
            game.delay(500)
            while true do
                local btnShow,x,y = checkZhuaguiBtn()
                if btnShow then
                    mlog.info("抓鬼领取按钮出现")
                    game.tap(x, y)
                    game.delay(800)
                    game.tap(x, y)
                    game.delay(500)
                    zhuaguiMgr.curCount = zhuaguiMgr.curCount + 1
                    if not game.IsDisplayDead(2000) then
                        mlog.info("开始抓鬼...当前抓鬼为："..tostring(zhuaguiMgr.curCount))
                        break
                    end
                else
                    game.delay(1000)
                end
            end
        end

        if game.IsDisplayDead(5000) then
            mlog.info("卡屏了，重新启动任务")
            zhuaguiStartFun()
            mlog.info("重新启动任务完成")
        end
    end
end

local function teamMemberMode()

    zhuaguiStartFun()
end

function zhuaguiMgr.execTask()
    mlog.info("抓鬼任务当前模式="..tostring(zhuaguiMgr.mode))
    if zhuaguiMgr.mode == 0 then
        teamLeaderMode()
    else
        while true do
            checkMemTeam()
            game.delay(2000)
        end
    end
end